package com.semidata.trp.material.config;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.semidata.rtd.core.config.DefaultValue;
import com.semidata.rtd.core.config.DefaultValueClass;
import com.semidata.rtd.core.config.DefaultValueHandler;
import com.semidata.rtd.core.config.Handler;
import com.semidata.rtd.core.config.Property;
import com.semidata.rtd.core.config.UrlHandler;

public class Config extends com.semidata.rtd.core.config.Config {

    private static String DEFAULT_CONFIG_FILE = "conf/material.conf";
    private static Config config = null;

    public static void init(String configFile) {
        if (configFile == null)
            configFile = Thread.currentThread().getContextClassLoader().getResource(DEFAULT_CONFIG_FILE).getPath();
        config = new Config(configFile);
    }

    public static class DefaultLogDir implements DefaultValueHandler {

        @Override
        public String value() {
            File file = new File(Thread.currentThread().getContextClassLoader().getResource(DEFAULT_CONFIG_FILE).getPath());
            file = new File(file.getParentFile().getParentFile(), "logs/material");
            return file.getAbsolutePath();
        }

    }

    public static class DefaultDataDir implements DefaultValueHandler {

        @Override
        public String value() {
            File file = new File(Thread.currentThread().getContextClassLoader().getResource(DEFAULT_CONFIG_FILE).getPath());
            file = new File(file.getParentFile().getParentFile(), "data/material");
            return file.getAbsolutePath();
        }

    }

    public static Config getConfig() {
        return config;
    }

    private Config(String configFile) {
        initConfig(configFile);
    }

    @Property("http.host")
    private String httpHost;

    @Property("http.port")
    @DefaultValue("6300")
    private int httpPort;

    @Property("http.threads")
    @DefaultValue("16")
    private int httpThreads;

    @Property("log.root")
    @DefaultValueClass(DefaultLogDir.class)
    private String logRoot;

    @Property("log.debug.enabled")
    @DefaultValue("false")
    private boolean logDebugEnabled;

    @Property("url.prefix")
    @DefaultValue("/material/")
    @Handler(UrlHandler.class)
    private String urlPrefix;

    @Property("material.path")
    @DefaultValueClass(DefaultDataDir.class)
    private String materialPath;

    @Property("type.jpg")
    @DefaultValue("image/jpeg")
    private String typeJpg;

    @Property("type.gif")
    @DefaultValue("image/gif")
    private String typeGif;

    @Property("type.bmp")
    @DefaultValue("application/x-bmp")
    private String typeBmp;

    @Property("type.png")
    @DefaultValue("image/png")
    private String typePng;

    @Property("type.swf")
    @DefaultValue("application/x-shockwave-flash")
    private String typeSwf;
    
    @Property("proxy.host")
    @DefaultValue("http://127.0.0.1")
    @Handler(UrlHandler.class)
    private String proxyHost;

    public String getProxyHost() {
		return proxyHost;
	}

	public String getHttpHost() {
        return httpHost;
    }

    public int getHttpPort() {
        return httpPort;
    }

    public int getHttpThreads() {
        return httpThreads;
    }

    public String getLogRoot() {
        return logRoot;
    }

    public boolean isLogDebugEnabled() {
        return logDebugEnabled;
    }

    public String getUrlPrefix() {
        return urlPrefix;
    }

    public String getMaterialPath() {
        return materialPath;
    }

    public Map<String, String> getAvailableImageTypes() {
        return new HashMap<String, String>() {
            private static final long serialVersionUID = 1L;

            {
                put("jpg", typeJpg);
                put("gif", typeGif);
                put("bmp", typeBmp);
                put("png", typePng);
                put("swf", typeSwf);
            }
        };
    }

    @Override
    public String toString() {
        return "Config [httpHost=" + httpHost + ", httpPort=" + httpPort + ", httpThreads=" + httpThreads + ", logRoot=" + logRoot + ", logDebugEnabled=" + logDebugEnabled + ", urlPrefix="
                + urlPrefix + ", materialPath=" + materialPath + ", typeJpg=" + typeJpg + ", typeGif=" + typeGif + ", typeBmp=" + typeBmp + ", typePng=" + typePng + ", typeSwf=" + typeSwf + "]";
    }

    public static void main(String[] args) {
        init(null);
        System.out.println(getConfig().toString());
    }
}